package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.SysSms;
import com.cls.business.service.ISysSmsService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.utils.MyUtil;
import com.github.pagehelper.PageInfo;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 短信功能表 Controller
 *
 * @author slx
 * @date 2020-12-17 11:07:59
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class SysSmsController extends BaseController {

    private final ISysSmsService sysSmsService;

    /**
     * 短信功能表列表页面
     **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/sysSms")
    public String sysSmsIndex() {
        return MyUtil.view("business/sysSms/sysSms");
    }

    /**
     * 短信功能表新增页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/sysSms/sysSmsAdd")
    public String sysSmsAdd(Model model) {
        return MyUtil.view("business/sysSms/sysSmsAdd");
    }

    /**
     * 短信功能表修改页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/sysSms/sysSmsUpdate/{sysSmsId}")
    public String sysSmsUpdate(@PathVariable Long sysSmsId, Model model) {
        SysSms sysSms = sysSmsService.getById(sysSmsId);
        model.addAttribute("sysSms", sysSms);
        return MyUtil.view("business/sysSms/sysSmsUpdate");
    }


    /**
     * 短信功能表详情页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/sysSms/sysSmsDetail/{sysSmsId}")
    public String sysSmsDetail(@PathVariable Long sysSmsId, Model model) {
        SysSms sysSms = sysSmsService.getById(sysSmsId);
        model.addAttribute("sysSms", sysSms);
        return MyUtil.view("business/sysSms/sysSmsDetail");
    }

    @GetMapping("sysSms")
    @ResponseBody
    public ResponseVO getAllSysSmss() {
        return ResponseVO.success();
    }

    @GetMapping("sysSms/list")
    @ResponseBody
//    @RequiresPermissions("sysSms:list")
    public ResponseVO sysSmsList(QueryRequest request, SysSms sysSms) {
        PageInfo<SysSms> pageInfo = this.sysSmsService.findSysSmss(request, sysSms);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增SysSms", exceptionMessage = "新增SysSms失败")
    @PostMapping("sysSms")
    @ResponseBody
    @RequiresPermissions("sysSms:add")
    public ResponseVO addSysSms(@Valid SysSms sysSms) {
        this.sysSmsService.createSysSms(sysSms);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除SysSms", exceptionMessage = "删除SysSms失败")
    @PostMapping("sysSms/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("sysSms:delete")
    public ResponseVO deleteSysSmss(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.sysSmsService.deleteSysSmss(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个SysSms", exceptionMessage = "删除SysSms失败")
    @PostMapping("sysSms/delete/{sysSmsId}")
    @ResponseBody
    @RequiresPermissions("sysSms:delete")
    public ResponseVO deleteSysSms(@PathVariable Long sysSmsId) {
        this.sysSmsService.removeById(sysSmsId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改SysSms", exceptionMessage = "修改SysSms失败")
    @PostMapping("sysSms/update")
    @ResponseBody
    @RequiresPermissions("sysSms:update")
    public ResponseVO updateSysSms(SysSms sysSms) {
        this.sysSmsService.updateSysSms(sysSms);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改SysSms", exceptionMessage = "导出Excel失败")
    @PostMapping("sysSms/excel")
    @ResponseBody
    @RequiresPermissions("sysSms:export")
    public void export(QueryRequest queryRequest, SysSms sysSms, HttpServletResponse response) {
        List<SysSms> sysSmss = this.sysSmsService.findSysSmss(queryRequest, sysSms).getList();
        ExcelKit.$Export(SysSms.class, response).downXlsx(sysSmss, false);
    }
}
